package com.ss.warfarin.service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.joda.time.Period;
import org.joda.time.PeriodType;

import com.ss.warfarin.persistence.dao.ProfileDao;
import com.ss.warfarin.persistence.model.Patient;

public class ProfileService {
	
	public Patient searchPatientProfileId(String patientId) {
		ProfileDao profileDao = new ProfileDao();
		return profileDao.searchPatient(patientId);
	}
	
	public Map<String, Integer> calculateAge(Patient patient) {
		
		// Convert patientBirthday to LocalDate
		DateTime birthdayDateTime = new DateTime(patient.getPatientBirthday().getTime());
		LocalDate birthdayDate = birthdayDateTime.toLocalDate();
		
		// Convert currentDate to LocalDate
		DateTime currentDateTime = new DateTime(new Date());
		LocalDate currentDate = currentDateTime.toLocalDate();

		Period period = new Period(birthdayDate, currentDate, PeriodType.yearMonthDay());
		
		Map<String, Integer> age = new HashMap<String, Integer>();
		age.put("years", period.getYears());
		age.put("months", period.getMonths());
		age.put("days", period.getDays());
		
		return age;
	}
	
}
